package com.metadata_system.system.response;

import com.alibaba.fastjson.JSON;
import com.metadata_system.common.db.entity.MetadataEdge;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class EdgeItem {
    private String id;
    private Integer et;
    private Integer metadataId;
    private Integer metadataViewId;
    private String uniqueKey;
    private String source;
    private String markerStart;
    private String markerEnd;
    private String target;
    private String type;
    private Object data;
    private List<Integer> viewIdArr;

    public static EdgeItem fromEntity(MetadataEdge metadataEdge) {
        HashMap<String, Object> data = new HashMap<>();

        int n1 = metadataEdge.getType() % 4;
        int n2 = metadataEdge.getType() / 4;

        // 客户端去填充
        data.put("label", metadataEdge.getName());
        data.put("lineType", "step");
        data.put("editFunction", false);

        data.put("edgeType", metadataEdge.getType());
        data.put("sourceHandle", metadataEdge.getSourceHandle());
        data.put("targetHandle", metadataEdge.getTargetHandle());
        data.put("properties", JSON.parseArray(StringUtils.isBlank(metadataEdge.getProperties()) ? "[]" : metadataEdge.getProperties()));

        EdgeItem edgeItem = EdgeItem.builder()
                .id("e" + metadataEdge.getId())
                .et(1)
                .metadataId(metadataEdge.getMetadataId())
                .metadataViewId(metadataEdge.getMetadataViewId())
                .uniqueKey(metadataEdge.getUniqueKey())
                .source("n" + metadataEdge.getSource())
                .target("n" + metadataEdge.getTarget())
                .type("custom")
                .markerStart("")
                .markerEnd("")
                .data(data)
                .viewIdArr(new ArrayList<>())
                .build();

//        // 开始箭头
//        if (n1 == 1 || n1 == 3) {
//            edgeItem.setMarkerStart("arrowclosed");
//        }
//        // 结束箭头
//        if (n1 == 2 || n1 == 3) {
//            edgeItem.setMarkerEnd("arrowclosed");
//        }


        return edgeItem;
    }

    public static List<EdgeItem> fromEntity(List<MetadataEdge> metadataEdges) {

        List<EdgeItem> list = new ArrayList<>();
        for (MetadataEdge metadataEdge : metadataEdges) {
            list.add(fromEntity(metadataEdge));
        }

        // 统计每个元素-名称对应的视图ID列表
        HashMap<String, List<Integer>> map = new HashMap<>();
        for (EdgeItem edgeItem : list) {
            List<Integer> viewIdList = map.get(edgeItem.getType());
            if (viewIdList == null) {
                viewIdList = new ArrayList<>();
            }
            if (!viewIdList.contains(edgeItem.getMetadataViewId())) {
                viewIdList.add(edgeItem.getMetadataViewId());
            }
            map.put(edgeItem.getType(), viewIdList);
        }


        // 设置视图ID数组
        for (EdgeItem edgeItem : list) {
            List<Integer> viewIdList = map.get(edgeItem.getType());
            edgeItem.setViewIdArr(viewIdList);
        }

        // 克隆关系-去重(根据type)
        HashMap<String, Boolean> isAddUniqueTypeItem = new HashMap<>();

        List<EdgeItem> newList = new ArrayList<>();
        for (EdgeItem edgeItem : list) {
            if (!isAddUniqueTypeItem.containsKey(edgeItem.getType())) {
                isAddUniqueTypeItem.put(edgeItem.getType(), true);
                newList.add(edgeItem);
            }
        }

        return newList;
    }
}
